package com.comac.ins.isfd.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.exception.base.BaseException;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.isfd.constant.enums.IsfdImportMethodEnum;
import com.comac.ins.isfd.domain.bo.IsfdAcousticsModelBo;
import com.comac.ins.isfd.domain.bo.query.IsfdAcousticsModelQueryBo;
import com.comac.ins.isfd.domain.vo.IsfdAcousticsModelVo;
import com.comac.ins.isfd.service.IIsfdAcousticsModelService;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 声学模型
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/isfd/acousticsModel")
public class IsfdAcousticsModelController extends BaseController {

    @Autowired
    private IIsfdAcousticsModelService isfdAcousticsModelService;

    /**
     * 查询声学模型列表
     */
    @SaCheckPermission("isfd:acousticsModel:list")
    @PostMapping("/list")
    public TableDataInfo<IsfdAcousticsModelVo> list(@RequestBody IsfdAcousticsModelQueryBo queryBo) {
        return isfdAcousticsModelService.queryPageList(queryBo);
    }

    /**
     * 获取声学模型详细信息
     */
    @SaCheckPermission("isfd:acousticsModel:query")
    @GetMapping("/{id}")
    public R<IsfdAcousticsModelVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long id) {
        return R.ok(isfdAcousticsModelService.queryById(id));
    }

    /**
     * 删除声学模型
     */
    @SaCheckPermission("isfd:acousticsModel:remove")
    @Log(title = "声学模型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(isfdAcousticsModelService.deleteWithValidByIds(List.of(ids)));
    }

    /**
     * 上传模型
     */
    @SaCheckPermission("isfd:acousticsModel:add")
    @Log(title = "上传模型", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/save")
    public R<Void> save(@RequestParam("file") MultipartFile file,
                        @NotEmpty(message = "支持的求解器不能为空") @RequestParam("solver") String solver,
                        @NotEmpty(message = "业务场景不能为空") @RequestParam("businessName") String businessName,
                        @RequestParam(required = false, value = "remarks") String remarks) {
        if (file == null || file.isEmpty()) {
            throw new BaseException("上传文件不能为空");
        }
        IsfdAcousticsModelBo bo = new IsfdAcousticsModelBo();
        bo.setSolver(solver);
        bo.setBusinessName(businessName);
        bo.setImportMethod(IsfdImportMethodEnum.SITE.getDesc());
        bo.setRemarks(remarks);
        // 文件名字相同直接覆盖
        return toAjax(isfdAcousticsModelService.saveOrUpdate(file, bo, Boolean.TRUE));
    }


    /**
     * 编辑
     */
    @SaCheckPermission("isfd:acousticsModel:add")
    @Log(title = "编辑", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/edit")
    public R<Void> edit(@RequestBody IsfdAcousticsModelBo bo) {
        return toAjax(isfdAcousticsModelService.edit(bo));
    }

    /**
     * 下载文件
     */
    @SaCheckPermission("isfd:acousticsModel:download")
    @GetMapping("/download/{id}")
    public void download(@PathVariable Long id, HttpServletResponse response) throws IOException {
        isfdAcousticsModelService.download(id, response);
    }

}
